<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>echarts地图</title>
    <script src="../js/data.js"></script>
    <script src="../js/echarts.min.js"></script>
    <script src="../js/jquery.min.js"></script>
</head>

<body>
<div class="viewport" id="home">
    <div class="echarts" id="echarts"></div>
</div>
</body>
<script>
    // 图标资源
    var icon1 = ''

    // 获取数据
    function getData() {
        $.get('../js/data.json', function (data) {
            getEcharts(data)
        });
    }

    // 渲染echarts
    function getEcharts(data) {
        var chartDom = document.getElementById('echarts');
        var myChart = echarts.init(chartDom);
        var nameMap = '地图数据';
        var geoCoordMap = {};
        var mapData = [];
        // 图标数据
        var iconData = [];
        echarts.registerMap(nameMap, data);
        /*获取地图数据*/
        myChart.showLoading();
        var mapFeatures = echarts.getMap(nameMap).geoJson.features;
        myChart.hideLoading();
        mapName = ''
        var mapInfo = []
        mapFeatures.forEach(function (v, index) {
            // 地区名称
            mapData.push({
                name: v.properties.name,
                value: Math.random() * 100
            });
            var name = v.properties.name;
            geoCoordMap[name] = v.properties.center;
            var data = {
                "value": v.properties.center,
                "id": index,
                "name": name,
            }
            iconData.push(data)
        });
        mapInfos = JSON.stringify(mapInfo)

// 生成飞线数据
        var fData = []
        for (var key in geoCoordMap) {
            for (var key1 in geoCoordMap) {
                if (key1 != key) {
                    fData.push({
                        coords: [
                            geoCoordMap[key],
                            geoCoordMap[key1],
                        ],
                        fromName: key,
                        toName: key1,
                    })
                    break
                }
            }
        }
        fData.splice(0, 1)

        var serverdata = [
            {
                type: 'lines',
                zlevel: 10000,
                effect: {
                    show: true,
                    period: 4, //箭头指向速度，值越小速度越快
                    trailLength: 0.1, //特效尾迹长度[0,1]值越大，尾迹越长重
                    symbol: 'arrow', //箭头图标
                    symbolSize: 4, //图标大小
                },
                tooltip: {
                    trigger: 'item',
                },
                label: {
                    show: false,
                    color: '#fff',
                    emphasis: {
                        color: 'white',
                        show: true
                    }
                },
                lineStyle: {
                    normal: {
                        color: '#23c1dc',
                        type: 'dashed',
                        width: 0.5, //尾迹线条宽度
                        opacity: 0.5, //尾迹线条透明度
                        curveness: 0.3 //尾迹线条曲直度
                    }
                },
                data: fData,
            },
            { // 地图块的相关信息
                type: 'map',
                map: nameMap,
                zoom: 1,
                roam: false,
                z: 1,
                aspectScale: 0.75,
                layoutCenter: ["50%", "50%"],
                layoutSize: '100%',

                itemStyle: {

                    normal: {
                        borderColor: 'rgba(147, 235, 248, 1)',
                        borderWidth: 0.5,
                        opacity: 1,
                        areaColor: {
                            type: 'linear-gradient',
                            x: 0,
                            y: 1200,
                            x2: 1000,
                            y2: 0,
                            colorStops: [{
                                offset: 0,
                                color: '#073d65' // 0% 处的颜色
                            }, {
                                offset: 1,
                                color: '#073d65' // 50% 处的颜色
                            }],
                            global: true // 缺省为 false
                        },


                    },
                    emphasis: {
                        areaColor: '#013159',
                        label: {
                            show: false,
                            color: 'rgba(255, 255, 255, 1)',
                            fontSize: 14
                        },
                    }
                },
                label: {
                    show: false,
                    normal: {
                        show: false,
                        textStyle: {
                            fontSize: 12,
                            fontWeight: 400,
                            color: '#fff'
                        }
                    }
                },
                data: mapData
            }
        ]

        // 生成地图图标
        iconData.forEach((type, index) => {
            var datamap = {
                type: 'scatter',
                tooltip: {
                    show: true,
                    formatter: function (params) {
                        return params.data.name;
                    }
                },
                name: type.name,
                coordinateSystem: 'geo',
                symbol: 'image://' + icon1,
                symbolSize: [60, 55],
                symbolOffset: [-0, -0],
                label: {
                    normal: {
                        show: true,
                        position: 'top',
                        offset: [0, 57],
                        formatter: function (params) {
                            var text = `{fline|${params.name}}`
                            return text
                        },
                        color: '#fff',
                        rich: {
                            fline: {
                                padding: [0, 0],
                                color: '#fff',
                                fontSize: 14,
                                fontWeight: 400,
                            },
                        },
                    },
                    emphasis: {
                        show: true,
                        position: 'top',
                        offset: [0, 62],
                        formatter: function (params) {
                            var text = `{fline|${params.name}}`
                            return text
                        },
                        color: '#fff',
                        rich: {
                            fline: {
                                padding: [0, 0],
                                color: '#fff',
                                fontSize: 14,
                                fontWeight: 400,
                            },
                        },
                    },
                },
                hoverAnimation: true,
                z: 6,
                data: [type]
            }
            serverdata.push(datamap)
        });
        var optionMap = {
            geo: {
                map: nameMap,
                show: true,
                aspectScale: 0.75,
                layoutCenter: ["50%", "51.5%"],
                layoutSize: '100%',
                roam: false,
                itemStyle: {
                    normal: {
                        borderColor: 'rgba(147, 235, 248, 1)',
                        borderWidth: 0.5,
                        areaColor: '#073d65',
                        opacity: 1,
                    },
                    emphasis: {
                        areaColor: '#2a333d'
                    }
                },
                z: 0,
                label: {
                    normal: {
                        show: true
                    },
                    emphasis: {
                        show: false
                    }
                }
            },
            series: serverdata
        };
        myChart.setOption(optionMap);
    }

    if (window.location.origin == 'file://') {
        setTimeout(() => {
            getEcharts(gdMap)
        }, 100)
    } else {
        getData()
    }
</script>
</html>
<style>
    html, body {
        padding: 0;
        margin: 0;
    }

    #home {
        width: 100%;
        height: 100%;
        background: #0d1551;
        position: fixed;
    }

    .echarts {
        width: 100%;
        height: 100%;
        background: #0d1551;
        position: fixed;
    }

    .info {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 10;
        color: red;
    }

    .links {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 10;

    }

    .links a {
        text-decoration: none;
        color: #fff;
    }
</style>
